package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.cat.EleAttr;
import com.yanqu.road.entity.activity.cat.Element;
import com.yanqu.road.entity.activity.cat.GameData;
import com.yanqu.road.entity.activity.cat.config.CatLottery;
import com.yanqu.road.entity.activity.cat.config.CatMissionInfo;
import com.yanqu.road.entity.activity.cat.config.CatNpcInfo;
import com.yanqu.road.entity.activity.cat.config.CatSword;
import com.yanqu.road.entity.activity.cat.data.CatUserData;
import com.yanqu.road.pb.activity.CatProto;
import com.yanqu.road.server.gameplayer.module.activity.cat.CatActivityConfig;
import com.yanqu.road.server.manger.ServerLanguageMgr;

import java.util.Map;

public class CatActivityPb {


    public static CatProto.CatUserDataTemp.Builder parseUserData(CatUserData userData) {
        CatProto.CatUserDataTemp.Builder builder = CatProto.CatUserDataTemp.newBuilder();
        builder.setScore(userData.getScore());
        builder.setPower(userData.getEnergy());
        builder.setLastRecoverPowerTime(userData.getLastRecTime());
        builder.setAbilityLv(userData.getAbilityLv());
        builder.setAbility(userData.getAbility());
        builder.setDamage(userData.getDamage());
        builder.setGameTimes(userData.getGameTimes());
        builder.setHistoryMaxPass(userData.getHistoryPassId());
        for (Map.Entry<Integer, Integer> entry : userData.getPassStarMap().entrySet()) {
            CatProto.CatPassStartTemp.Builder startBuilder = CatProto.CatPassStartTemp.newBuilder();
            startBuilder.setPass(entry.getKey());
            startBuilder.setStar(entry.getValue());
            builder.addPassStar(startBuilder);
        }

        CatProto.CatGameDataTemp.Builder gameData = getCellDataBuilder(userData.getCellData());
        builder.setGameData(gameData);
        builder.setStatus(parseCatGameStatusTemp(userData.getGameData()));
        return builder;
    }

    public static CatProto.CatGameDataTemp.Builder getCellDataBuilder(Element[][] elements) {
        CatProto.CatGameDataTemp.Builder gameData = CatProto.CatGameDataTemp.newBuilder();
        for (int i = 0; i < CatActivityConfig.ROW_NUM; i++) {
            for (int k = 0; k < CatActivityConfig.COLUMN_NUM; k++) {
                Element element = elements[k][i];
                if(element != null){
                    gameData.addCell(parseCellData(element));
                }
            }
        }
        return gameData;
    }

    public static CatProto.CatGameCellDataTemp.Builder parseCellData(Element element) {
        CatProto.CatGameCellDataTemp.Builder builder = CatProto.CatGameCellDataTemp.newBuilder();
        CatProto.CatGameCellElementTemp.Builder eleBuilder = CatProto.CatGameCellElementTemp.newBuilder();
        eleBuilder.setType(element.getType());
        eleBuilder.setX(element.getX());
        eleBuilder.setY(element.getY());
        eleBuilder.setId(element.getId());
        eleBuilder.setColor(element.getColor());
        eleBuilder.setVal(element.getVal());
        for (EleAttr attr : element.getAttrs()) {
            CatProto.CatGameCellAttrTemp.Builder attrBuilder = CatProto.CatGameCellAttrTemp.newBuilder();
            attrBuilder.setType(attr.getType());
            attrBuilder.setVal(attr.getVal());
            attrBuilder.setId(attr.getId());
            attrBuilder.setColor(attr.getColor());
            builder.addAttr(attrBuilder);
        }
        builder.setElement(eleBuilder);
        return builder;
    }

    public static CatProto.CatGameStatusTemp.Builder parseCatGameStatusTemp(GameData gameData){
        CatProto.CatGameStatusTemp.Builder builder = CatProto.CatGameStatusTemp.newBuilder();
        builder.setRoleBlood(gameData.getRoleBlood());
        builder.setRoleX(gameData.getRoleX());
        builder.setRoleY(gameData.getRoleY());
        builder.setAllRound(gameData.getAllRound());
        builder.setRound(gameData.getRound());
        builder.setDifficulty(gameData.getDifficulty());
        builder.setPass(gameData.getPass());
        builder.setBossBlood(gameData.getBossBlood());
        builder.setAngerBar(gameData.getAngerBar());
        builder.setSettle(gameData.isSettle());
        builder.setDamage(gameData.getDamage());
        builder.setBossId(gameData.getBossId());
        return builder;
    }

    public static CatProto.CatMissionConfigTemp.Builder parseCatMissionTemp(CatMissionInfo missionInfo, String language) {
        CatProto.CatMissionConfigTemp.Builder builder = CatProto.CatMissionConfigTemp.newBuilder();
        builder.setId(missionInfo.getId());
        builder.setChapterId(missionInfo.getChapterId());
        builder.setType(missionInfo.getType());
        builder.setEnergyCost(missionInfo.getEnergyCost());
        builder.setIsBoss(missionInfo.getIsBoss());
        builder.setMap(missionInfo.getMap());
        builder.setChapterName(ServerLanguageMgr.getContent(missionInfo.getChapterName(), language));
        builder.setChapterDesc(ServerLanguageMgr.getContent(missionInfo.getChapterDesc(), language));
        builder.setName(ServerLanguageMgr.getContent(missionInfo.getName(), language));
        builder.setRecommendPower(missionInfo.getRecommendPower());
        builder.setRoundsNumber(missionInfo.getRoundsNumber());
        builder.setMonsterNumber(missionInfo.getMonsterNumber());
        builder.setBossId(missionInfo.getBossId());
        builder.setBossPower(missionInfo.getBossPower());
        builder.setRewards(missionInfo.getRewards());
        builder.setSuccessRewards(missionInfo.getSuccessRewards());
        builder.setSuccessRandomRewards(missionInfo.getSuccessRandomRewards());
        builder.setFailRewards(missionInfo.getFailRewards());
        builder.setMonsterId(missionInfo.getMonsterId());
        return builder;
    }

    public static CatProto.CatNpcConfigTemp.Builder parseCatNpcTemp(CatNpcInfo npcInfo, String language) {
        CatProto.CatNpcConfigTemp.Builder builder = CatProto.CatNpcConfigTemp.newBuilder();
        builder.setId(npcInfo.getId());
        builder.setIcon(npcInfo.getIcon());
        builder.setName(ServerLanguageMgr.getContent(npcInfo.getName(), language));
        builder.setDesc(ServerLanguageMgr.getContent(npcInfo.getDesc(), language));
        builder.setSkillType(npcInfo.getSkillType());
        builder.setSkillParameter(npcInfo.getSkillParameter());
        return builder;
    }

    public static CatProto.CatLotteryConfigTemp.Builder parseCatLotteryTemp(CatLottery lottery, String language) {
        CatProto.CatLotteryConfigTemp.Builder builder = CatProto.CatLotteryConfigTemp.newBuilder();
        builder.setId(lottery.getId());
        builder.setName(ServerLanguageMgr.getContent(lottery.getName(), language));
        builder.setWeights(lottery.getWeights());
        builder.setRewards(lottery.getRewards());
        builder.setIsBroadcast(lottery.getIsBroadcast());
        builder.setSort(lottery.getSort());
        return builder;
    }

    public static CatProto.CatSwordConfigTemp.Builder parseCatSword(CatSword sword, String language) {
        CatProto.CatSwordConfigTemp.Builder builder = CatProto.CatSwordConfigTemp.newBuilder();
        builder.setLevel(sword.getLevel());
        builder.setCost(sword.getCost());
        builder.setAtk(sword.getAtk());
        return builder;
    }

    public static CatProto.CatConnectPointTemp.Builder parsePoint(int x, int y){
        CatProto.CatConnectPointTemp.Builder builder = CatProto.CatConnectPointTemp.newBuilder();
        builder.setX(x);
        builder.setY(y);
        return builder;
    }

    public static CatProto.CatConnectDetailTemp.Builder parseCatConnectDetailTemp(int x, int y, int eleType, int eleId, int value, int attackType){
        CatProto.CatConnectDetailTemp.Builder builder = CatProto.CatConnectDetailTemp.newBuilder();
        builder.setPoint(parsePoint(x, y));
        builder.setType(eleType);
        builder.setId(eleId);
        builder.setAttackType(attackType);
        builder.setValue(value);
        return builder;
    }

    public static CatProto.CatConnectDetailTemp.Builder parseCatConnectDetailTemp(int x, int y, int eleType, int eleId, int value, int targetX, int targetY){
        CatProto.CatConnectDetailTemp.Builder builder = parseCatConnectDetailTemp(x, y, eleType, eleId, value, 1);
        builder.setTarget(parsePoint(targetX, targetY));
        return builder;
    }
}
